Release 10.1A: OpenEdge Development:
Messaging and ESB


Publishing and subscribing with a TextMessage

The procedures example1.p and example2.p (1 of 2) demonstrate basic Pub/Sub messaging. The procedure example1.p publishes a TextMessage to a topic, and the procedure example2.p (1 of 2) subscribes to a topic and receives a TextMessage.

To run example1.p and example2.p:

  1. Run example2.p (1 of 2) so the subscriber is running before you publish, as shown:
  2. example2.p
    /* Subscribes and receives a Text message. */ 
    DEFINE VARIABLE pubsubsession AS HANDLE. 
    DEFINE VARIABLE consumerH AS HANDLE. 
    /* Creates a session object. */ 
    RUN jms/pubsubsession.p PERSISTENT SET pubsubsession("-H localhost -S 
    5162 "). 
    RUN setBrokerURL IN pubsubsession ("localhost:2506"). 
    RUN beginSession IN pubsubsession. 
    /* Subscribe to the GolfTopic topic. Messages are handled by the 
       "golfHandler" internal procedure. 
    */ 
    RUN createMessageConsumer IN pubsubsession ( 
                      THIS-PROCEDURE, /* This proc will handle it */ 
                      "golfHandler",  /* name of internal procedure */ 
                      OUTPUT consumerH). 
    RUN subscribe IN pubsubsession (
                  "GolfTopic",     /* name of topic */ 
                  ?,               /* Subscription is not durable */ 
                  ?,               /* No message selector */ 
                  no,              /* Want my own messages too */ 
                  consumerH).      /* Handles the incoming messages*/ 
    /* Start receiving messages */ 
    RUN startReceiveMessages IN pubsubsession. 
    /* Wait to receive the messages. Any other IO-blocked statements can be
       used for receiving messages.  
    */ 
    WAIT-FOR u1 OF THIS-PROCEDURE. 
    
    PROCEDURE golfHandler: 
    DEFINE INPUT PARAMETER messageH AS HANDLE.  
    DEFINE INPUT PARAMETER msgConsumerH AS HANDLE. 
    DEFINE OUTPUT PARAMETER replyH AS HANDLE. 
        /* Display the message - we assume that reply is not required. */ 
        DISPLAY "Message text: " DYNAMIC-FUNCTION('getText':U IN messageH) 
          FORMAT "x(70)". 
        RUN deleteMessage IN messageH. 
        APPLY "U1" TO THIS-PROCEDURE.  
    END. 
    

  3. Run example1.p to publish the TextMessage to a topic, as shown:
  4. example1.p
    /* Publishes A Text message. */ 
    DEFINE VARIABLE pubsubsession AS HANDLE. 
    DEFINE VARIABLE messageH AS HANDLE. 
    /* Creates a session object. */ 
    RUN jms/pubsubsession.p PERSISTENT SET pubsubsession  
                                      ("-H localhost -S 5162 "). 
    RUN setBrokerURL IN pubsubsession ("localhost:2506"). 
    RUN beginSession IN pubsubsession. 
    /* Create a text message */ 
    RUN createTextMessage IN pubsubsession (OUTPUT messageH).  
    RUN setText IN messageH ("Golf shoes on sale today"). 
    /* Publish the message on the "GolfTopic" topic */ 
    RUN publish IN pubsubsession ("GolfTopic", messageH, ?, ?, ?). 
    RUN deleteMessage IN messageH. 
    RUN deleteSession IN pubsubsession. 
    


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095